<?php

namespace app\store\controller;
use think\Request;
use think\Validate;
use think\Db;

class Beautician extends ApiCommon
{
	//美容师列表
    public function Beauticianlist()
    {
    	//商店详情
    	$request = Request::instance();
		$data = $request->param();
		
		$rule = [
		    'storeid'=> 'require|number',
		];
			
		$msg = [
		    'storeid.require' => 'storeid not null',
		    'storeid.number'     => 'storeid is number'
		];
		
		$validate = new Validate($rule, $msg);
		$result   = $validate->check($data);
    	
		if(!$result){
			$this->returnMsg('-1',$validate->getError());
		}
		
		$storeid = $data['storeid'];		
		$bresult = Db::query('select * from ml_beautician where storeid=? and isshow=1',[$storeid]);	
		$rolemsg = Db::name('beautician_role')->where(['storeid'=>$storeid])->select();
		
		$this->returnMsg('ok','',['barr'=>$bresult,'role'=>$rolemsg]);
	}
	
	//美容师详细信息
	public function BeauticianMsg(){
		//商店详情
    	$request = Request::instance();
		$data = $request->param();
		
		$rule = [
		    'bid'  => 'require|number'
		];
			
		$msg = [
		    'bid.require' => 'bid not null',
		    'bid.number'     => 'bid is number'
		];
		
		$validate = new Validate($rule, $msg);
		$result   = $validate->check($data);
    	
		if(!$result){
			return $this->ErrorSend($validate->getError());
		}
		$bid = $data['bid'];
		
		$bresult = Db::query('select * from ml_beautician where beautician_id = ?',[$bid]);
		
		return json_encode(['error'=>'ok','arr'=>$bresult]);
	}

	//美容师对应项目
	public function BeauticianGoods(){
		//商店详情
    	$request = Request::instance();
		$data = $request->param();
		
		$rule = [
		    'bid'  => 'require|number'
		];
			
		$msg = [
		    'bid.require' => 'bid not null',
		    'bid.number'     => 'bid is number'
		];
		
		$validate = new Validate($rule, $msg);
		$result   = $validate->check($data);
    	
		if(!$result){
			return $this->ErrorSend($validate->getError());
		}
		$bid = $data['bid'];
		
		$bresult = Db::query('select id,gname,message,price,icon,type,topicon from ml_goods where id in (select goodsid from ml_beauticianskill where bid = ?)',[$bid]);
		
		return json_encode(['error'=>'ok','arr'=>$bresult]);
	}
	
	//商品对应技师表
	public function GoodsBeautician(){
		
		//$ctl = CONTROLLER_NAME;
    	//$act = ACTION_NAME;
		//return '$ctl='.$ctl.'--$act='.$act;;

		
		
		//商店详情
    	$request = Request::instance();
		$data = $request->param();
		
		$rule = [
		    'goodsid'  => 'require|number'
		];
			
		$msg = [
		    'goodsid.require' => 'goodsid not null',
		    'goodsid.number'     => 'goodsid is number'
		];
		
		$validate = new Validate($rule, $msg);
		$result   = $validate->check($data);
    	
		if(!$result){
			return $this->ErrorSend($validate->getError());
		}
		$goodsid = $data['goodsid'];
		
		$bresult = Db::query('select * from ml_beautician where beautician_id in (select bid from ml_beauticianskill where goodsid = ?)',[$goodsid]);
		
		return json_encode(['error'=>'ok','arr'=>$bresult]);
	}

	//预约
	public function bespeak(){
		$btime = input('btime/d',0);
		$bid = input('bid/d',0);
		$uid = input('uid/d',0);
		$data = input('post.');
		
		$storeid = 1;//session('storeid');
		if($storeid<=0){
			$this->returnMsg('400','未登陆');
		}
		
		$data['storeid'] = $storeid;
		if($bid<=0 || $uid<=0){
			$this->returnMsg('-1','美体师不存在');
		}
		
		//Db::query("select * from ml_beautician where beautician_id=28");//
		$bres = Db::name('beautician')->where(['beautician_id'=>$bid])->select();
		if(count($bres)<=0){
			$this->returnMsg('-1','美体师不存在..');
		}
		
		$hbres = Db::name('bespeak')->where(['bid'=>$bid,'uid'=>$uid,'btime'=>$btime])->select();
		if(count($hbres)>0){
			$this->returnMsg('-1','对不起,您该时间段已经预约过');
		}
		
//		exit('$hbres='.count($hbres).'$bid='.$bid.'$uid='.$uid.'btime='.$btime);
		
		Db::name('bespeak')->add($data);
		$this->returnMsg('ok','预约成功');
	}
	
	//用户预约列表
	public function beapeakList(){
		$uid = input('uid/d',0);
		$page = input('page/d',0);		
		$pageSize = config('PageSize');
		$startpage = $page*$pageSize;
		if($uid<=0){
			$this->returnMsg('-1','用户信息不存在');
		}
		
		$blist = Db::query('select ml_bespeak.*,ml_beautician.name from ml_bespeak,ml_beautician where ml_bespeak.bid = ml_beautician.beautician_id 
		and ml_bespeak.uid = $uid limit $startpage,$pageSize');
		
		$this->returnMsg('ok','',$blist);
	}
	
	//技师预约信息
	public function beapeakInfo(){
		//1.擅长技能
		$bid = input('bid/d',0);
		
		$bres = Db::query("select * from ml_beautician where beautician_id = $bid");
		$skillstr = $bres[0]['skill'];
		if(strlen($skillstr)<=0){
			$this->returnMsg('ok','',[]);
		}
		
		$skill = Db::query("select * from ml_store_skill where id in($skillstr)");
		$this->returnMsg('ok','',$skill);
	}
}

?>